package com.kisyou.blogs;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class 文章列表所有 {
    public static void main(String[] args) throws SQLException {
        List<Blog> blogList = new ArrayList<>();

        try (Connection c = DBUtil.connection()) {
            String sql = "select bid, title, published_at, author from blogs order by published_at desc";
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                try (ResultSet rs = ps.executeQuery()) {
                    while (rs.next()) {
//                        int bid = rs.getInt(1);
                        int bid = rs.getInt("bid");
                        String title = rs.getString("title");
                        String publishedAt = rs.getString("published_at");
                        String author = rs.getString("author");

                        Blog blog = new Blog(bid, title, publishedAt, author);
                        blogList.add(blog);
                    }
                }
            }
        }

        // 具体如何展示
        System.out.printf("一共 %d 条博客\n", blogList.size());
        for (Blog blog : blogList) {
            System.out.println(blog);
        }
    }
}